package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChooseLotteryActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;

import java.util.List;

/**
 * Created by ASUS on 2017/11/13.
 */

public class ChooseLotteryGridAdapter extends BaseAdapter {
    private List<LotteryStatOneResponse.ItemListBean.ItemsBean> itemsBeen;
    private Context mContext;
    private ChooseLotteryActivity chooseLotteryActivity;

    public ChooseLotteryGridAdapter(List<LotteryStatOneResponse.ItemListBean.ItemsBean> itemsBeen, Context context, ChooseLotteryActivity chooseLotteryActivity) {
        super();
        this.itemsBeen = itemsBeen;
        mContext = context;
        this.chooseLotteryActivity = chooseLotteryActivity;
    }

    @Override
    public int getCount() {
        return  itemsBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return  itemsBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.choose_category_item, null, false);
            holder.name = convertView.findViewById(R.id.name);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(itemsBeen.get(position).getNameX());

//        convertView.setOnClickListener(null);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LotteryStatOneResponse.ItemListBean.ItemsBean itemsBean = itemsBeen.get(position);

                Intent intent = new Intent();
                intent.putExtra("code", itemsBeen.get(position).getCode());
                intent.putExtra("name", itemsBeen.get(position).getNameX());
                intent.putExtra("item_class", itemsBeen.get(position).getItem_class());
                chooseLotteryActivity.setResult(111, intent);
                chooseLotteryActivity.finish();
            }
        });
        if(itemsBeen.get(position).getCode().equals(chooseLotteryActivity.getIntent().getStringExtra("code"))){
            convertView.setBackgroundResource(R.drawable.bg_corners_100_3);
            holder.name.setTextColor(Color.parseColor("#ffffff"));
        } else {
            convertView.setBackgroundResource(R.drawable.bg_corners_100_12);
            holder.name.setTextColor(Color.parseColor("#3f3f3f"));
        }

        return convertView;

    }

    static class ViewHolder {
        TextView name;
    }
}
